//
//  AllocateRoomProcess.m
//  QRMaster
//
//  Created by myApple on 16/6/1.
//  Copyright © 2016年 myApple. All rights reserved.
//

#import "AllocateRoomProcess.h"
#import "ItemModel.h"
#import "AllocateRoomModel.h"
#import "setTimeModel.h"
#import "RoomDataProcess.h"
#import "AssignRoomTimeModel.h"
#import "AssignFloorTimeModel.h"
#import "AssignBuildTimeModel.h"

@interface AllocateRoomProcess()
{
    NSMutableArray * bulidingArray;
   __block NSMutableArray * allocatedRoomArray;
   __block NSMutableArray * floorAllocateArray;//整层的房间都已经分配了 装起楼层ID
    __block NSMutableArray * buildAllocateArray;
    //整栋楼的分配房间和总房间是否相同
    NSInteger roomSelect;
    NSInteger roomSum;
}
@end

@implementation AllocateRoomProcess

-(NSMutableArray *)dataProcess:(NSArray *)data
{
    bulidingArray = [[NSMutableArray alloc]init];
    allocatedRoomArray = [[NSMutableArray alloc]init];
    floorAllocateArray = [[NSMutableArray alloc]init];
    buildAllocateArray = [[NSMutableArray alloc]init];
    NSMutableArray * floorArray = [[NSMutableArray alloc]init];
    NSMutableArray * roomArray = [[NSMutableArray alloc]init];
    for (NSDictionary * dict in data) {
        roomSelect = 0;
        roomSum = 0;
        ItemModel * bmodel = [[ItemModel alloc]init];
        bmodel.itemId = [dict objectForKey:@"buildingid"];
        bmodel.itemNo = [dict objectForKey:@"buildingno"];
        bmodel.itemName = [dict objectForKey:@"buildingname"];
        bmodel.itemChildrenArray = [dict objectForKey:@"floors"];
        bmodel.nodeLevel = 1;
        bmodel.nodeType = NodeTypeGroup;
        bmodel.isClick = NO;
        bmodel.nodeId = [dict objectForKey:@"buildingid"];
        if (bmodel.itemChildrenArray) {
            [floorArray removeAllObjects];
            for (NSDictionary * fdict in bmodel.itemChildrenArray) {
                ItemModel * fmodel = [[ItemModel alloc]init];
                fmodel.itemId = [fdict objectForKey:@"floorid"];
                fmodel.itemName = [fdict objectForKey:@"floorname"];
                fmodel.itemNo = [fdict objectForKey:@"floorno"];
                fmodel.itemChildrenArray = [fdict objectForKey:@"rooms"];
                fmodel.nodeLevel = 2;
                fmodel.isClick = NO;
                fmodel.nodeType = NodeTypeGroup;
                fmodel.nodeId = [dict objectForKey:@"floorid"];
                if (fmodel.itemChildrenArray) {
                    [roomArray removeAllObjects];
                    for (NSDictionary * rdict in fmodel.itemChildrenArray) {
                        AllocateRoomModel * rmodel = [[AllocateRoomModel alloc]init];
                        rmodel.roomid = [rdict objectForKey:@"roomid"];
                        rmodel.roomName = [rdict objectForKey:@"roomname"];
                        rmodel.roomno = [rdict objectForKey:@"roomno"];
                        rmodel.isSelect = [[rdict objectForKey:@"isSelected"]intValue];//是否被分配的房间
                        rmodel.nodeLevel = 3;
                        rmodel.nodeType = NodeTypeItem;
                        rmodel.nodeId = [dict objectForKey:@"roomid"];
                        roomSum ++;
                        if (rmodel.isSelect == 1) {
                            fmodel.itemSelect ++;
                            [allocatedRoomArray addObject:rmodel.roomid];
                            roomSelect ++;
                        }
                        fmodel.itemSum ++;
                        [roomArray addObject:rmodel];
                    }
                    if (roomArray.count > 0) {
                        bmodel.itemSum ++;
                        if (fmodel.itemSelect > 0) {
                            bmodel.itemSelect ++;
                        }
                        fmodel.itemChildrenArray = [roomArray copy];
                        [floorArray addObject:fmodel];
                    }
                    //判断整层房间是否都分配了
                    if (fmodel.itemSum == fmodel.itemSelect) {
                        [floorAllocateArray addObject:fmodel.itemId];
                    }
                }
                
            }
            if (floorArray.count > 0) {
                bmodel.itemChildrenArray = [floorArray copy];
                [bulidingArray addObject:bmodel];
            }
            if (roomSum == roomSelect) {
                [buildAllocateArray addObject:bmodel.itemId];
            }
        }
    }
    return bulidingArray;
}

#pragma mark -------获取新模型的房间
-(NSMutableArray *)getRoomDataAllocate
{
    NSMutableArray * roomArray = [NSMutableArray array];
    for (ItemModel * bModel in bulidingArray) {
        for (ItemModel * fModel in bModel.itemChildrenArray) {
            for (AllocateRoomModel * rModel in fModel.itemChildrenArray) {
                AssignRoomTimeModel * rTModel = [[AssignRoomTimeModel alloc]init];
                rTModel.roomId = rModel.roomid;
                rTModel.roomNo  = rModel.roomno;
                rTModel.roomName = rModel.roomName;
                rTModel.bulidingName = bModel.itemName;
                rTModel.floorName = fModel.itemName;
                rTModel.timeArray = rModel.timeArray;
                [roomArray addObject:rTModel];
            }
        }
    }
    return roomArray;
}

#pragma mark ------获取新模型的楼层
-(NSMutableArray *)getFloorDataAllocate
{
    NSMutableArray * floorArray = [NSMutableArray array];
    for (ItemModel * bModel in bulidingArray) {
        for (ItemModel * fModel in bModel.itemChildrenArray) {
            AssignFloorTimeModel * model = [[AssignFloorTimeModel alloc]init];
            model.floorId = fModel.itemId;
            model.floorName = fModel.itemName;
            model.bulidingName = bModel.itemName;
            model.selectRoom = fModel.itemSelect;
            model.totalRoom = fModel.itemSum;
            [floorArray addObject:model];
        }
    }
    return floorArray;
}

#pragma mark ------获取新模型的楼栋
-(NSMutableArray *)getBuildDataAllocate
{
    NSMutableArray * buildArray = [NSMutableArray array];
    for (ItemModel * bModel in bulidingArray) {
        AssignBuildTimeModel * model = [[AssignBuildTimeModel alloc]init];
        model.buildingid = bModel.itemId;
        model.buildingno = bModel.itemNo;
        model.bulidingName = bModel.itemName;
        model.sumRoom = bModel.itemSum;
        model.floorArray = bModel.itemChildrenArray;
        [buildArray addObject:model];
    }
    return buildArray;
}

#pragma mark - 获取已经分配的所有房间
-(NSMutableArray *)getAllocatedRoom
{
    return allocatedRoomArray;
}

#pragma mark - 获取整层房间都分配的所有楼层ID
-(NSMutableArray *)getFloorAllocated
{
    return floorAllocateArray;
}

#pragma mark - 获取整栋房间都分配的所有楼栋ID
-(NSMutableArray *)getBuildAllocated
{
    return buildAllocateArray;
}

#pragma mark - 整个集群的所有选中房间ID 通过楼层
-(NSMutableArray *)getAllRoomIdOfFloorId:(NSArray *)floorIdArray withBuildArray:(NSArray *)buildArray
{
    NSMutableArray * allRoomIdArray = [[NSMutableArray alloc]init];
    for (ItemModel * bModel in buildArray) {
        //楼栋下面的楼层
        for (ItemModel * fModel in bModel.itemChildrenArray) {
            //判断楼层是否和选中的楼层相同
            NSInteger i;
            for (i = 0 ; i < floorIdArray.count; i++) {
                NSString * floorId = floorIdArray[i];
                if ([fModel.itemId isEqualToString:floorId]) {
                    for (AllocateRoomModel * rModel in fModel.itemChildrenArray) {
                        [allRoomIdArray addObject:rModel.roomid];
                    }
                    break;
                }
            }
            if (i >= floorIdArray.count) {
                //楼层ID不同，就加入一些已经选中的房间
                for (AllocateRoomModel * rModel in fModel.itemChildrenArray) {
                    if (rModel.isSelect) {
                        [allRoomIdArray addObject:rModel.roomid];
                    }
                }
            }
        }
    }
    return allRoomIdArray;
}

#pragma mark - 整个集群的所有选中房间ID 通过楼栋
-(NSMutableArray *)getAllRoomIdOfBuildId:(NSArray *)buildIdArray withBuildArray:(NSArray *)buildArray
{
    NSMutableArray * allRoomIdArray = [[NSMutableArray alloc]init];
    for (ItemModel * bModel in buildArray) {
        NSInteger i ;
        for (i = 0; i < buildIdArray.count; i ++) {
            NSString * buildId = buildIdArray[i];
            if ([bModel.itemId isEqualToString:buildId]) {
                for (ItemModel * fModel in bModel.itemChildrenArray) {
                    for (AllocateRoomModel * rModel in fModel.itemChildrenArray) {
                        [allRoomIdArray addObject:rModel.roomid];
                    }
                }
                break;
            }
            if (i >= buildIdArray.count) {
                for (ItemModel * fModel in bModel.itemChildrenArray) {
                    for (AllocateRoomModel * rModel in fModel.itemChildrenArray) {
                        if (rModel.isSelect) {
                            [allRoomIdArray addObject:rModel.roomid];
                        }
                    }
                }
            }
        }
    }
    return allRoomIdArray;
}

#pragma mark - 两个数组元素是否相同
-(BOOL)arrayItemIsSame:(NSArray *)firstArray withArray:(NSArray *)secondeArray
{
    NSArray * newFirstArray = [firstArray sortedArrayUsingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
        return [obj1 compare:obj2];
    }];
    
    NSArray * newSecondeArray = [secondeArray sortedArrayUsingComparator:^NSComparisonResult(NSString * obj1, NSString * obj2) {
        return [obj1 compare:obj2 ];
    }];
    
    return [newFirstArray isEqualToArray:newSecondeArray];
}

@end
